package com.easyfun.healthmagicbox.util;

import java.util.ArrayList;
import java.util.HashMap;

import com.easyfun.healthmagicbox.R;

import android.app.Activity;  
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.BaseAdapter;  
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;  



public class CustomDialogAdapter extends BaseAdapter   {


	 private String[] list;  
	    // 用来控制CheckBox的选中状况  
	 private static HashMap<Integer, Boolean> isSelected;  
	    // 上下文  
	 private Context context;  
	 private LayoutInflater inflater = null;  

	public CustomDialogAdapter(Activity context, String[] list) {  
	    super();  
	    this.context = context; 
	    this.list = list;
	    isSelected = new HashMap<Integer, Boolean>(); 
	    initData();
	}  

	private void initData() {
		  for (int i = 0; i < list.length; i++) {  
	            getIsSelected().put(i, false);  
	      }  
	}
	public int getCount() {  
	    // TODO Auto-generated method stub  
	    return list.length;  
	}  

	public Object getItem(int position) {  
	    // TODO Auto-generated method stub  
	    return list[position];  
	}  

	public long getItemId(int position) {  
	    // TODO Auto-generated method stub  
	    return position;  
	}  

	
	 //You have to overide the getView method
	@Override
	public View getView(int position, View convertView, ViewGroup parent)  
	{  
	    //added row, to convert view
	    View row = convertView; 
	    ViewContainer holder = null;  
	    
	    if (row == null)  
	    {  
	        inflater =  ((Activity)this.context).getLayoutInflater();    
	        row = inflater.inflate(R.layout.custom_dialog_data, parent, false);
	        
	        holder = new ViewContainer();  
            // 导入布局并赋值给convertview  
            holder.tv = (TextView) row.findViewById(R.id.item_tv);  
            holder.cb = (CheckBox) row.findViewById(R.id.item_cb);  
            // 为view设置标签  
            row.setTag(holder);  
	    } else{
	    	holder = (ViewContainer) convertView.getTag();  
	    }
	    holder.tv.setText(list[position]); 
	    holder.cb.setChecked(getIsSelected().get(position));  

       
	return row;  
	}  
	
	public static HashMap<Integer, Boolean> getIsSelected() {  
        return isSelected;  
    }  
	public static final class ViewContainer {  
        public TextView tv;  
        public CheckBox cb;  
    }  
}  
	
